package com.shiguo365.market.display.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.shiguo365.market.R;
import com.shiguo365.market.presenter.contract.IAddressListPresenter;
import com.shiguo365.market.presenter.implement.AddressListPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by caomenglong on 16/7/12.
 */
public class AddressListAdapter extends RecyclerView.Adapter<AddressListAdapter.ViewHolder>{
    public final String TAG="AddressListAdapter";
    List<JSONObject> jsonObjectList = new ArrayList<JSONObject>();
    IAddressListPresenter iAddressListPresenter;
    private String mold=null;

    public AddressListAdapter(IAddressListPresenter iAddressListPresenter, List<JSONObject> jsonObjectList) {
        this.jsonObjectList = jsonObjectList;
        this.iAddressListPresenter =iAddressListPresenter;
        this.mold=iAddressListPresenter.getMold();
        Log.d(TAG,this.mold);
    }
    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_user_address,viewGroup,false);
        ViewHolder vh = new ViewHolder(view,this.mold);
        return vh;
    }
    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        Log.d("AddressListActivity",jsonObjectList.get(position).toString());
        viewHolder.update(jsonObjectList.get(position));

    }
    //获取数据的数量
    @Override
    public int getItemCount() {
        return jsonObjectList.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public  class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tv_name;
        public TextView tv_phone;
        public TextView tv_address;
        private String mMold=null;

        @BindView(R.id.user_address_item_btn_set_default)
        Button btn_set_default;


        @BindView(R.id.textViewdefaultTip) TextView textViewdefaultTip;

        JSONObject object;

        public ViewHolder(View view,String mold){
            super(view);
            ButterKnife.bind(this, itemView);
            tv_name = (TextView) view.findViewById(R.id.user_address_item_tv_name);
            tv_phone = (TextView) view.findViewById(R.id.user_address_item_tv_phone);
            tv_address = (TextView) view.findViewById(R.id.user_address_item_tv_address);
            this.mMold=mold;
            Log.d(TAG,this.mMold+"11111");
            if(this.mMold.equals("FromOrderConfirm")){
                btn_set_default.setText("选择该地址");
            }
        }

        @OnClick(R.id.user_address_item_btn_delete)
        protected void delbtn(){
            iAddressListPresenter.delAddressById(object.getInteger("id"));
        }

        @OnClick(R.id.user_address_item_btn_set_default)
        protected void setdefault(){
            iAddressListPresenter.setAddressDefaultById(object.getInteger("id"));
            if(this.mMold.equals("FromOrderConfirm")){
                iAddressListPresenter.onOrderConfrimSelectedOK();
            }


        }

        public void update(JSONObject object){
            this.object=object;
            this.tv_name.setText(object.getString("name"));
            this.tv_phone.setText(object.getString("phone"));
            this.tv_address.setText(object.getString("province")+object.getString("city")+object.getString("street"));

            if(object.getInteger("is_default")==1){
                textViewdefaultTip.setVisibility(View.VISIBLE);
                btn_set_default.setVisibility(View.GONE);
            }else {
                textViewdefaultTip.setVisibility(View.GONE);
                btn_set_default.setVisibility(View.VISIBLE);
            }

        }
    }
}
